package com.bytech.common.biz.entity;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytech.common.utils.ApplicationBeanUtils;

/**
 * 分页对象统一父类
 *
 * @Author Zhao
 * @Create 2021/10/11 11:14
 */
public class IPageQueryBase<S> extends Page<S> {

    private static final long serialVersionUID = -6291901266586982430L;

    /**
     * records 向下转型
     *
     * @param targetClass 目标子类 S:分页对象的父类泛型（MP分页泛型对象）
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public <T extends S> Page<T> translateRecordsTo(Class<T> targetClass) {
        Page<T> result = ApplicationBeanUtils.copyProperties(this, Page.class);
        List<T> newRecords = ApplicationBeanUtils.copyProperties(this.getRecords(), targetClass);
        result.setRecords(newRecords);
        return result;
    }
}
